<?php
/**
 *
 * Author: Lyu
 * Date: 2023/7/22
 * Time: 17:55
 */

namespace App\Service\Core\Admin\Menu;

use App\Model\Core\Admin\AdminMenuModel;
use App\Service\Core\CoreBaseService;
use App\Traits\SingleInstanceTrait;

class AdminMenuService extends CoreBaseService
{
    use SingleInstanceTrait;

    public function getOne($id): array
    {
        return AdminMenuModel::getInstance()->getOne((int)$id);
    }

    /**
     * 获取所有父级
     * @param $parent_id
     * @return array
     */
    public function getParents($parent_id): array
    {
        $parents = [];
        $parent = AdminMenuModel::getInstance()->getOne((int)$parent_id);
        if ($parent) {
            $parents[] = $parent;
            if ($parent['parent_id']) {
                $item = $this->getParents($parent['parent_id']);
                $parents = [...$parents, ...$item];
            }
        }
        return $parents;
    }

    public static function getMenuTreeList($parent_id = 0): array
    {
        $list = [];
        $m_list = AdminMenuModel::getInstance()->getAllMenu();
        foreach ($m_list as $item) {
            if ($item['parent_id'] == $parent_id) {
                $sub_list = self::getMenuTreeList($item['id']);
                if (!empty($sub_list)) {
                    $item['children'] = $sub_list;
                }
                $item['meta'] = json_decode($item['meta'], true);
                $item['parentId'] = $item['parent_id'];
                $list[] = $item;
            }
        }
        return $list;
    }

    public static function getMenuTreeByIds($m_list = [], $parent_id = 0): array
    {
        $list = [];
        foreach ($m_list as $item) {
            if ($item['parent_id'] == $parent_id) {
                $sub_list = self::getMenuTreeByIds($m_list, $item['id']);
                if (!empty($sub_list)) {
                    $item['children'] = $sub_list;
                }
                $item['meta'] = json_decode($item['meta'], true);
                $item['parentId'] = $item['parent_id'];
                $list[] = $item;
            }
        }
        return $list;
    }

    /**
     *
     * @param $dragging_id
     * @param $drop_id
     * @param $drop_type
     * @param $operate_id
     * @param $operate_by
     * @return array
     * @throws \Throwable
     */
    public function draggingChange($dragging_id, $drop_id, $drop_type, $operate_id, $operate_by): array
    {
        $type = ['inner', 'before', 'after'];
        if (empty($dragging_id) || empty($drop_id) || empty($drop_type)) {
            return alert_info(1, '数据异常请刷新页面');
        }
        if (empty($operate_id) || empty($operate_by)) {
            return alert_info(1, '操作人不存在');
        }
        if (!in_array($drop_type, $type)) {
            return alert_info(1, '不支持的类型');
        }
        $dragging_info = AdminMenuModel::getInstance()->getOne((int)$dragging_id);
        $drop_info = AdminMenuModel::getInstance()->getOne((int)$drop_id);
        if (empty($dragging_info)) {
            return alert_info(1, '拖拽菜单不存在');
        }
        if (empty($drop_info)) {
            return alert_info(1, '释放菜单不存在');
        }
        $data = [];
        $sort_order = 0;
        $parent_id = $drop_type == 'inner' ? $drop_id : $drop_info['parent_id'];
        $sub_menu_list = AdminMenuModel::getInstance()->getSubMenuList($parent_id);
        foreach ($sub_menu_list as $item) {
            if ($item['id'] == $dragging_id) continue;//如果是同级调整 跳过当前菜单
            //拖拽当前之前
            if ($drop_type == 'before' && $item['id'] == $drop_id) {
                $data[$dragging_id] = ['sort_order' => ++$sort_order];
            }
            //当前元素
            $data[$item['id']] = ['sort_order' => ++$sort_order];
            //拖拽当前之后
            if ($drop_type == 'after' && $item['id'] == $drop_id) {
                $data[$dragging_id] = ['sort_order' => ++$sort_order];
            }
        }
        if ($drop_type == 'inner') {//inner 不可能回是同级
            $data[$dragging_id] = ['sort_order' => ++$sort_order];
        }
        $connection = AdminMenuModel::getInstance()->getConnection();
        $connection->beginTransaction();
        foreach ($data as $id => $item) {
            $item['parent_id'] = $parent_id;
            $item['updated_at'] = date('Y-m-d H:i:s');
            $item['updated_by'] = $operate_by;
            $res = AdminMenuModel::getInstance()->updateOne($id, $item);
            if ($res === false) {
                $connection->rollBack();
                return alert_info('1', '操作失败');
            }
        }
        $connection->commit();
        return alert_info(0, '操作成功');
    }

    /**
     * 删除菜单
     * @param $ids
     * @param $operate_id
     * @param $operate_by
     * @return array
     * @throws \Throwable
     */
    public function delete($ids, $operate_id, $operate_by): array
    {
        if (empty($ids) || !is_array($ids)) {
            return alert_info(1, '数据错误');
        }
        if (empty($operate_id) || empty($operate_by)) {
            return alert_info(1, '操作人不存在');
        }
        $connection = AdminMenuModel::getInstance()->getConnection();
        $connection->beginTransaction();
        foreach ($ids as $id) {
            $menu_info = AdminMenuModel::getInstance()->getOne($id);
            if (empty($menu_info)) continue;
            $res = AdminMenuModel::getInstance()->deleteOne($id);
            if ($res === false) {
                $connection->rollBack();
                return alert_info(1, '删除失败');
            }
            $this->addLog('菜单管理', '删除菜单【' . $menu_info['name'] . '】', $operate_id, $operate_by);
        }
        $connection->commit();
        return alert_info(0, '删除成功');
    }
}
